package com.ms.game.bank.db;

import com.ms.game.bank.message.BankResponse;
import com.ms.game.common.container.GameLimitInfo;
import com.ms.player.Player;
import com.ms.service.error.ErrorCode;
import com.ms.service.module.account.Account;
import com.ms.service.module.account.db.AccountDAO;
import com.ms.service.module.datacenterlite.CentralAsyncCommand;
import com.ms.service.module.zone.module.BasicZoneModule;
import com.xpec.c4.game.time.Stamper;
import com.xpec.c4.game.utility.GUID;
import com.xpec.c4.service.message.MessageAction;
import com.xpec.c4.service.module.BasicServiceModule;
import com.xpec.c4.service.module.common.PlayerCommonInfoManager;

public class checkAndCreatePasswordDCLAsync extends CentralAsyncCommand {
	
	private static final long serialVersionUID = 3322753526059592290L;

	/**
	 * 現在修改的密碼
	 */
	public String password;
	
	public GUID accountID;
	
	public GUID sessionID;
	
	public int messageType;
	
	public boolean isFormLogin;
	
	private String bankPassword;
	
	private int limitCode;

	/** 是否為機器人 */
	private boolean robot;

	private boolean isSave;

	@Override
	public void asyncExecute() throws Exception {
		
		Account accountData = AccountDAO.loadAccountByAccountId(accountID);
		
		if(isFormLogin){
			Object[] lvInfo = AccountDAO.getAccountMaxLvAndViplv(accountID.toString());
			//確認會員限制 
			limitCode = GameLimitInfo.checkLimitByClassify(GameLimitInfo.BANK_SET_PASSWORD, (int)lvInfo[0], (int)lvInfo[1], accountData.isVerifiedMember(), accountData.isTrialAccount(), accountData.getUplineCode());
			if(limitCode != GameLimitInfo.TRUE){
				return;
			}		
		}
		
		bankPassword = accountData.getBankPassword();
		if(accountData.getBankPassword() != "" && accountData.getBankPassword() != null){
			//密碼已經創建
			return;
		}

		// 把使用者密碼加密
		String encryptPassword = Stamper.getInstance().Save(password);
		isSave = BankDAO.UpdateBankPassword(accountID, encryptPassword);
		if (isSave) {
			password = encryptPassword;
		}
	}

	@Override
	public void callBackExecute(Object... userData) throws Exception {
		
		BankResponse response = new BankResponse(); 
		
		if(isFormLogin){
			if(limitCode != GameLimitInfo.TRUE){
				response.errorCode = GameLimitInfo.limitToErrorCode(limitCode);
				MessageAction.sendMessage(sessionID, messageType, response);
				return;
			}		
		}
		
		if(bankPassword != "" && bankPassword != null){
			//密碼已經創建
			response.errorCode = ErrorCode.PASSWORD_CREATE_ALREADY;
			MessageAction.sendMessage(sessionID, messageType, response);
			return;
		}
		
		if(!isSave){
			response.errorCode = ErrorCode.DB_OPERATION_FAIL;
			MessageAction.sendMessage(sessionID, messageType, response);
			return;
		}

		// 如果是在 zone 上, 把有密碼的資訊寫回 player
		BasicServiceModule module = getCurrentModuleServer();
		if (module instanceof BasicZoneModule) {
			BasicZoneModule zm = (BasicZoneModule) module;
			GUID playerId = PlayerCommonInfoManager.getInstance().getPlayerGuidBySession(sessionID);
			Player player = zm.getZone().getPlayer(playerId);
			if (player != null) {
				player.setHaveBankPassword(true);
			}
		}

		response.errorCode = ErrorCode.SUCCESS;
		MessageAction.sendMessage(sessionID, messageType, response);
		
	}

	public void setRobot(boolean robot) {
		this.robot = robot;
	}

}
